package com.thinkit.bigdata.web.service.impl.config;

import com.thinkit.bigdata.core.feature.orm.mybatis.Page;
import com.thinkit.bigdata.web.dao.config.SysResourceMapper;
import com.thinkit.bigdata.web.model.config.SysResource;
import com.thinkit.bigdata.web.model.config.SysResourceExample;
import com.thinkit.bigdata.web.service.config.ResourceService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by lihongli on 2018/9/1
 */
@Service
public class ResourceServiceImpl implements ResourceService {

    @Resource
    private SysResourceMapper mapper;

    @Override
    public List<SysResource> selectByExample(SysResourceExample example) {
        return mapper.selectByExample(example);
    }

    @Override
    public List<SysResource> selectByExample(Page<SysResource> page, SysResourceExample example) {
        return mapper.selectByExample(page, example);
    }

    @Override
    public Integer insert(SysResource record) {
        return mapper.insertSelective(record);
    }

    @Override
    public SysResource resourceGetByPK(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public List<SysResource> parentResourceAll(SysResourceExample example) {
        return mapper.selectByExample(example);
    }

    @Override
    public Integer update(SysResource record) {
        return mapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public Integer resourceDelete(Long id) {
        return resourceDel(id);
    }

    @Override
    public Integer resourceDeleteBatch(List<Long> idList) {
        int rowNum = 0;
        for (Long id : idList) {
            rowNum = resourceDel(id);
        }
        return rowNum;
    }

    @Override
    public Integer statusSingle(SysResource record, Long id) {
        return resourceStatus(record, id);
    }

    @Override
    public Integer statusBatch(SysResource record, List<Long> idList) {
        int rowNum = 0;
        for (Long id : idList) {
            rowNum = resourceStatus(record, id);
        }
        return rowNum;
    }

    /**
     * 根据id进行递归删除
     *
     * @param id
     */
    private Integer resourceDel(Long id) {
        // 先删除id对应的resource
        Integer rowNum = mapper.deleteByPrimaryKey(id);
        // 获取parentId为id的所有resource
        SysResourceExample example = new SysResourceExample();
        example.createCriteria().andParentidEqualTo(id);
        List<SysResource> list = mapper.selectByExample(example);
        for (SysResource resource : list) {
            resourceDel(resource.getId());
        }
        return rowNum;
    }

    /**
     * 根据id进行递归修改状态
     * @param id
     * @return
     */
    private Integer resourceStatus(SysResource record, Long id){
        // 先修改id自身对应的资源
        SysResourceExample example = new SysResourceExample();
        example.createCriteria().andIdEqualTo(id);
        int rowNum = mapper.updateByExampleSelective(record,example);
        // 再修改id下级资源
        SysResourceExample example2 = new SysResourceExample();
        example2.createCriteria().andParentidEqualTo(id);
        List<SysResource> list = mapper.selectByExample(example2);
        for (SysResource res : list) {
            resourceStatus(record, res.getId());
        }
        return rowNum;
    }
}
